package com.csxy.six.controller;

import com.csxy.six.dto.LostItemDTO;
import com.csxy.six.entity.LostItem;
import com.csxy.six.service.LostItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/lost-items")
public class LostItemController {

    private final LostItemService lostItemService;

    @Autowired
    public LostItemController(LostItemService lostItemService) {
        this.lostItemService = lostItemService;
    }

    @PostMapping
    public ResponseEntity<LostItem> addLostItem(@RequestBody LostItemDTO lostItemDTO) {
        LostItem addedItem = lostItemService.addLostItem(lostItemDTO);
        return ResponseEntity.ok(addedItem);
    }

    @GetMapping
    public ResponseEntity<List<LostItem>> getAllLostItems() {
        List<LostItem> items = lostItemService.getAllLostItems();
        return ResponseEntity.ok(items);
    }

    @GetMapping("/{lostId}")
    public ResponseEntity<LostItem> getLostItemById(@PathVariable Integer lostId) {
        LostItem item = lostItemService.getLostItemById(lostId);
        return ResponseEntity.ok(item);
    }

    @PutMapping("/{lostId}")
    public ResponseEntity<LostItem> updateLostItem(
            @PathVariable Integer lostId,
            @RequestBody LostItemDTO lostItemDTO) {
        LostItem updatedItem = lostItemService.updateLostItem(lostId, lostItemDTO);
        return ResponseEntity.ok(updatedItem);
    }

    @DeleteMapping("/{lostId}")
    public ResponseEntity<Void> deleteLostItem(@PathVariable Integer lostId) {
        lostItemService.deleteLostItem(lostId);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/publisher/{publisherId}")
    public ResponseEntity<List<LostItem>> getLostItemsByPublisher(@PathVariable Long publisherId) {
        List<LostItem> items = lostItemService.getLostItemsByPublisher(publisherId);
        return ResponseEntity.ok(items);
    }

    @PatchMapping("/{lostId}/found")
    public ResponseEntity<LostItem> markAsFound(@PathVariable Integer lostId) {
        LostItem item = lostItemService.markAsFound(lostId);
        return ResponseEntity.ok(item);
    }
}